package com.contact;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Formatter;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringTool {
	

	/**
	 * 
	 * @Description:TODO返回文件路径，末尾带文件分割符
	 * @param@param fullPathName "/mnt/sdcard/DCIM/Camera/1376702054396.jpg"
	 * @param@return
	 * @return String /mnt/sdcard/DCIM/Camera/
	 * @author zhuw
	 * @date 2013-8-17 上午10:08:53
	 */
	public static String getLocalFilePath(String fullPathName) {
		if (ObjTool.isNotNull(fullPathName)) {
			return fullPathName.substring(0, fullPathName.lastIndexOf("/") + 1)
					.toLowerCase();
		} else {
			return null;
		}
	}

	/**
	 * 
	 * @Description:TODO返回文件名称
	 * @param@param fullPathName "/mnt/sdcard/DCIM/Camera/1376702054396.jpg"
	 * @param@return
	 * @return String 1376702054396.jpg
	 * @author zhuw
	 * @date 2013-8-17 上午10:08:53
	 */
	public static String getLocalFileName(String fullPathName) {
		if (ObjTool.isNotNull(fullPathName)) {
			return fullPathName.substring(fullPathName.lastIndexOf("/") + 1)
					.toLowerCase();
		} else {
			return null;
		}
	}
	/**
	 * 
	 * @Description:TODO格式化输出刷新时间
	 * @return
	 * @return String
	 * @author zhuw
	 * @date 2013-5-21 下午3:03:43
	 */
	public static String getFreshTime() {
		return new SimpleDateFormat("MM-dd HH:mm").format(new Date());

	}
	/**
	 * 
	 * @return获取当前日期
	 */
	public static String getNowDate() {
		SimpleDateFormat sDateFormat = new SimpleDateFormat(
				"yyyy-MM-dd hh:mm:ss");
		return sDateFormat.format(new java.util.Date());

	}
	/**
	 * 
	 * @Description: 将long时间戳转换成“年-月-日”格式
	 * @param timeMillisStr
	 * @return
	 */
	public static String timeToStrDay(String timeMillisStr) {
		return myTimeStr(Long.parseLong(timeMillisStr));

	}

	/**
	 * 
	 * @Description:TODO
	 * @return
	 * @return String
	 * @author zhuw
	 * @date 2013-4-26 上午11:47:22
	 */
	public static String getSystemTime() {
		StringBuffer sb = new StringBuffer();
		Calendar c = Calendar.getInstance();
		sb.append(c.get(Calendar.HOUR_OF_DAY)).append(":")
				.append(c.get(Calendar.MINUTE));
		return sb.toString();
	}

	/**
	 * 
	 * @Description: 转换时间
	 * @param timeMs
	 * @return
	 */
	public static String stringForTime(long timeMs) {
		StringBuilder mFormatBuilder = new StringBuilder();
		Formatter mFormatter = new Formatter(mFormatBuilder,
				Locale.getDefault());
		int totalSeconds = (int) (timeMs / 1000);

		int seconds = totalSeconds % 60;
		int minutes = (totalSeconds / 60) % 60;
		int hours = totalSeconds / 3600;

		mFormatBuilder.setLength(0);
		if (hours > 0) {
			return mFormatter.format("%d:%02d:%02d", hours, minutes, seconds)
					.toString();
		} else {
			return mFormatter.format("%02d:%02d", minutes, seconds).toString();
		}
	}

	/**
	 * 
	 * @Description: 将long时间戳转换成“年-月-日”格式
	 * @param timeMillis
	 * @return
	 */
	public static String myTimeStr(long timeMillis) {
		int timezone = 8;
		long totalSeconds = timeMillis / 1000;
		totalSeconds += 60 * 60 * timezone;
		int second = (int) (totalSeconds % 60);// 秒
		long totalMinutes = totalSeconds / 60;
		int minute = (int) (totalMinutes % 60);// 分
		long totalHours = totalMinutes / 60;
		int hour = (int) (totalHours % 24);// 时
		int totalDays = (int) (totalHours / 24);

		int _year = 1970;

		int year = _year + totalDays / 366;
		int month = 1;
		int day = 1;

		int diffDays;
		boolean leapYear;
		while (true) {
			int diff = (year - _year) * 365;
			diff += (year - 1) / 4 - (_year - 1) / 4;
			diff -= ((year - 1) / 100 - (_year - 1) / 100);
			diff += (year - 1) / 400 - (_year - 1) / 400;

			diffDays = totalDays - diff;

			leapYear = (year % 4 == 0) && (year % 100 != 0)
					|| (year % 400 == 0);
			if (!leapYear && diffDays < 365 || leapYear && diffDays < 366) {
				break;
			} else {
				year++;
			}
		}

		int[] monthDays;
		if (diffDays >= 59 && leapYear) {
			monthDays = new int[] { -1, 0, 31, 60, 91, 121, 152, 182, 213, 244,
					274, 305, 335 };
		} else {
			monthDays = new int[] { -1, 0, 31, 59, 90, 120, 151, 181, 212, 243,
					273, 304, 334 };
		}
		for (int i = monthDays.length - 1; i >= 1; i--) {
			if (diffDays >= monthDays[i]) {
				month = i;
				day = diffDays - monthDays[i] + 1;
				break;
			}
		}

		return new StringBuffer().append(year).append("-").append(month)
				.append("-").append(day).append(" ").toString();

	}

	/**
	 * 
	 * @Description: 判断url是否是图片
	 * @param url
	 * @return
	 */
	public static boolean isPic(String url) {
		boolean flag = false;
		if (ObjTool.isNotNull(url)) {
			if (url.endsWith(".jpg") || url.endsWith(".png")) {
				flag = true;
			}
		}
		return flag;
	};

	/**
	 * 
	 * @Description:TODO字符串时间格式转时间戳 yyyy-MM-dd HH:mm:ss
	 * @param strDate
	 * @return
	 * @throws ParseException
	 * @return long
	 * @author zhuw
	 * @date 2013-5-29 下午3:41:56
	 */
	public static long strDateToTimeStamp(String strDate) throws ParseException {
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(strDate)
				.getTime();

	}

	/**
	 * 
	 * @Description:TODOString数组转List
	 * @param arrayData
	 * @return
	 * @return ArrayList<String>
	 * @author zhuw
	 * @date 2013-4-28 下午4:35:32
	 */
	public static ArrayList<String> stringArrayToList(String[] arrayData) {
		ArrayList<String> list = null;
		if (arrayData != null && arrayData.length > 0) {
			list = new ArrayList<String>();
			for (int i = 0; i < arrayData.length; i++) {
				list.add(arrayData[i]);

			}

		}
		return list;
	}

	/**
	 * 
	 * @Description:TODO获取网页域名
	 * @param url
	 * @return
	 * @return String
	 * @author zhuw
	 * @date 2013-5-23 上午9:37:01
	 */
	public static String getDomainName(String url) {
		String result = null;
		Pattern p = Pattern.compile(
				"(?<=http://|\\.)[^.]*?\\.(com|cn|net|org|biz|info|cc|tv)",
				Pattern.CASE_INSENSITIVE);
		Matcher m = p.matcher(url);
		if (m.find()) {
			result = m.group();
		}
		return result;

	}

	/**
	 * 
	 * @Description:TODO获取一段文字中的网页链接，只返回以http://开头的地址，只返回第一个
	 * @param sourceStr
	 * @return
	 * @return String
	 * @author zhuw
	 * @date 2013-5-30 下午3:38:34
	 */
	public static String getUrl(String sourceStr) {
		String result = null;
		Matcher m = Pattern.compile("(?i)http://[^\u4e00-\u9fa5]+").matcher(
				sourceStr);
		if (m.find()) {
			result = m.group();
		}
		return result;
	}

	/**
	 * 
	 * @Description:TODO判断一个日期是否是今天
	 * @param str日期格式
	 *            "yyyy-MM-dd"
	 * @return
	 * @throws Exception
	 * @return boolean
	 * @author zhuw
	 * @date 2013-5-9 下午2:43:15
	 */
	public static boolean checkIsToday(Calendar cal, String str)
			throws Exception {
		boolean flag = false;
		if (ObjTool.isNotNull(str)) {
			int y = cal.get(Calendar.YEAR);
			int m = cal.get(Calendar.MONTH) + 1;
			int d = cal.get(Calendar.DATE);
			String stra[] = str.split("-");
			flag = (y == Integer.parseInt(stra[0]))
					&& (m == Integer.parseInt(stra[1]))
					&& (d == Integer.parseInt(stra[2]));
		}
		return flag;

	}

	/**
	 * 
	 * @Description:TODO获取父栏目路径
	 * @param@param fullPath 精品栏目_成都味道_李伯清
	 * @param@return精品栏目_成都味道
	 * @return String
	 * @author zhuw
	 * @date 2013-6-18 下午5:20:56
	 */
	public static String getParentPath(String fullPath) {
		String result = "";
		if (ObjTool.isNotNull(fullPath)) {
			if (fullPath.contains("_")) {
				result = fullPath.substring(0, fullPath.lastIndexOf("_"));
			} else {
				result = fullPath;
			}
		}
		return result;

	}

	/**
	 * 
	 * @Description:TODO获取父栏目名称
	 * @param@param fullPath
	 * @param@return
	 * @return String
	 * @author zhuw
	 * @date 2013-6-18 下午5:58:30
	 */
	public static String getParentCatName(String fullPath) {
		String result = "";
		if (ObjTool.isNotNull(fullPath)) {
			if (fullPath.contains("_")) {
				result = fullPath.substring(fullPath.lastIndexOf("_") + 1);
			} else {
				result = fullPath;
			}
		}
		return result;
	}

	/**
	 * 
	 * @Description:TODO验证邮箱
	 * @param@param strEmail
	 * @param@return
	 * @return boolean
	 * @author zhuw
	 * @date 2013-11-21 上午11:36:05
	 */
	public static boolean checkEmail(String strEmail) {
		// String strPattern = "\\w+@\\w+\\.(com\\.cn)|\\w+@\\w+\\.(com|cn)";
		Pattern p = Pattern.compile("[\\w[.-]]+@[\\w[.-]]+\\.[\\w]+");
		Matcher m = p.matcher(strEmail);
		return m.matches();
	}

	/**
	 * 
	 * @Description:TODO验证电话号码
	 * @param@param phone
	 * @param@return
	 * @return boolean
	 * @author zhuw
	 * @date 2013-11-21 上午11:36:13
	 */
	public static boolean validateMoblie(String phone) {
		Pattern p = Pattern
				.compile("^((13[0-9])|(147)|(15[^4,\\D])|(18[0-9]))\\d{8}$");
		Matcher m = p.matcher(phone);
		return m.matches();
	}

	/**
	 * 
	 * @Description:TODO
	 * @param@param str 要转换的字符串
	 * @param@return
	 * @return int 异常情况返回-1
	 * @author yjj
	 * @date 2013-12-10 上午11:17:15
	 */
	public static int strIntoInt(String str) {
		int i = 0;
		try {
			i = Integer.parseInt(str);
		} catch (NumberFormatException e) {
			i = -1;
			
		}
		return i;
	}

	/**
	 * 
	 * @Description:TODO
	 * @param@param str 要转换的字符串
	 * @param@return
	 * @return double异常情况返回-1.0
	 * @author yjj
	 * @date 2013-12-10 上午11:17:15
	 */
	public static double strIntoDouble(String str) {
		double i = 0.0;
		try {
			i = Double.parseDouble(str);
		} catch (NumberFormatException e) {
			i = -1.0;
		
		}
		return i;
	}

	/**
	 * 
	 * @Description:TODO
	 * @param@param str 要转换的字符串
	 * @param@return
	 * @return long 异常情况返回-1L
	 * @author yjj
	 * @date 2013-12-10 上午11:17:15
	 */
	public static long strIntoLong(String str) {
		long i = 0L;
		try {
			i = Long.parseLong(str);
		} catch (NumberFormatException e) {
			i = -1L;
		
		}
		return i;
	}

	/**
	 * 
	 * @Description:TODO
	 * @param@param str 要转换的字符串
	 * @param@return
	 * @return float
	 * @author yjj
	 * @date 2013-12-10 上午11:17:15
	 */
	public static float strIntoFloat(String str) {
		float i = 0.0f;
		try {
			i = Float.parseFloat(str);
		} catch (NumberFormatException e) {
			i = -1.0f;
			
		}
		return i;
	}

	public static String mobileEncry(String mobile) {
		StringBuffer sb = new StringBuffer("");
		if (ObjTool.isNotNull(mobile)) {
			sb.append(mobile.substring(0, 4)).append("***")
					.append(mobile.substring(9));
		}
		return sb.toString();
	}

	/**
	 * 
	 * @Description:TODOUnicode转汉字
	 * @param@param str
	 * @param@return
	 * @return String
	 * @author zhuw
	 * @date 2013-11-7 上午9:57:21
	 */
	public static String unicodeToString(String str) {
		Pattern pattern = Pattern.compile("(\\\\u(\\p{XDigit}{4}))");

		Matcher matcher = pattern.matcher(str);

		char ch;

		while (matcher.find()) {

			ch = (char) Integer.parseInt(matcher.group(2), 16);

			str = str.replace(matcher.group(1), ch + "");

		}

		return str;

	}
	
	
	public static boolean isImg(String url){
		if(ObjTool.isNotNull(url)&&url.startsWith("http")&&(url.endsWith("jpg")||url.endsWith("JPG")||url.endsWith("PNG")||url.endsWith("png"))){
			return true;
		}else{
			return false;
		}
	}
}
